﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Xsl;
using System.Xml.Linq;

using System.Collections.ObjectModel;

using EzDesk.Utility;
using System.IO;
using System.Xml;

namespace EzDesk.Clients.Utility
{
    public class FeedTransformBase : IFeedConvertion
    {
        protected XslCompiledTransform _transform = new XslCompiledTransform();
        protected string _namespace = "http://www.codeplex.com/ezdeskdev";

        public FeedTransformBase(string xsltUri)
        {
            // 从嵌入的资源中获得xslt
            using (var stream = this.GetType().Assembly.GetManifestResourceStream(xsltUri))
            {
                using (XmlReader reader = XmlReader.Create(stream))
                {
                    _transform.Load(reader);
                }
            }            
        }
        public void Transform(FeedClientModel model, params KeyValuePair<string, string>[] keyValues)
        {
            model.TransformedData = new FeedDataModel()
                                        {
                                            Title = Transform(model.OriginalData.Title, keyValues),
                                            Body = Transform(model.OriginalData.Body, keyValues)
                                        };
        }

        public string Transform(string transformStr, params KeyValuePair<string, string>[] keyValues)
        {
            return XmlHelper.TranslateXml(_transform, transformStr, keyValues).Trim();
        }

        public void Transform(IList<FeedClientModel> models, params KeyValuePair<string, string>[] keyValues)
        {
            models.Foreach(m => this.Transform(m, keyValues));
        }
    }
}
